package net.neptech.service.impl;

import lombok.extern.slf4j.Slf4j;
import net.neptech.feign.ScheduleFeignClient;
import net.neptech.model.appointment.ClnicStaff;
import net.neptech.model.schedule.ScheShift;
import net.neptech.model.schedule.Schedule;
import net.neptech.model.vo.ScheduleToClnic;
import net.neptech.repository.ClnicStaffRepository;
import net.neptech.service.IClnicStaffService;
import net.neptech.service.IScheduleToClnicService;
import net.neptech.topics.ClinicStaffTopics;
import net.neptech.utils.BeanCopyUtils;
import net.neptech.utils.DateUtils;
import net.neptech.vm.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class ScheduleToClnicServiceImpl implements IScheduleToClnicService {

    @Autowired
    private ScheduleFeignClient scheduleFeignClient;
    @Autowired
    private ClnicStaffRepository clnicStaffRepository;
    @Autowired
    private KafkaTemplate<String, ClnicStaff> kafkaTemplate;
    @Autowired
    private IClnicStaffService  iClnicStaffService;
    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;







    @Value("${jobs.daynum}")
    private Integer dayNum;
    /**
     * 排班各坐诊转换
     * @param schedule
     * @return
     */
    public List<ScheduleToClnic> getScheduleToClnicList(Schedule schedule) {
        ScheShift scheShift = schedule.getScheShift();
        schedule.setScheShift(scheShift);
        ResponseResult<List<Schedule>> listResponseResult
                = scheduleFeignClient.searchSchedule(schedule);
        log.info("排班信息:{}",listResponseResult.getData());
        return  listResponseResult.getData().stream().
                map(ScheduleToClnic::new).toList();
    }

    /**
     * 数据库插入数据
     * @param scheduleToClnics
     * @return
     */
    public List<ClnicStaff> insertClnicStaff(List<ScheduleToClnic> scheduleToClnics) {
        List<ClnicStaff> clnicStaffs = BeanCopyUtils.
                copyBeanList(scheduleToClnics, ClnicStaff.class);
        //批量插入 采用for循环
        List<ClnicStaff> result = new ArrayList<>();
        clnicStaffs.forEach(clnicStaff -> {
            try {
                ClnicStaff save = clnicStaffRepository.save(clnicStaff);
                iClnicStaffService.putSlot(clnicStaff);
                //
                kafkaTemplate.send(ClinicStaffTopics.PUT_SLOT,"putSlot",save);
                result.add(clnicStaff);
            } catch (Exception e) {
                log.info(e.getMessage());
            }
        });
        return result ;
    }
    @Override
    /**
     * 全部插入
     *
     */
    public List<ClnicStaff> convert() {
        Schedule schedule = new Schedule();
        ScheShift scheShift = new ScheShift();
        scheShift.setShiftName("坐诊");
        schedule.setScheShift(scheShift);
        List<ScheduleToClnic> list = getScheduleToClnicList(schedule);
        List<ClnicStaff> clnicStaffs = insertClnicStaff(list);
        list.forEach(System.out::println);
        return  clnicStaffs;
    }



    /**
     * 根据未来第几天插入
     * @param dayNum
     * @return
     */
    @Override
    public List<ClnicStaff> timeConvert(int dayNum) {
        //获取当前时间
        Date now = new Date();
        //获取日
        Date futureDate = DateUtils.getFutureDate(now,dayNum);
        //时间转换只留日
        futureDate.setHours(0);
        futureDate.setMinutes(0);
        futureDate.setSeconds(0);
        log.info("当前时间:{}",futureDate);
        // 获取查询
        Schedule schedule = new Schedule();
        ScheShift scheShift = new ScheShift();
        scheShift.setShiftName("坐诊");
        schedule.setDutyDay(futureDate);
        schedule.setScheShift(scheShift);
        List<ScheduleToClnic> scheduleToClnicList
                = getScheduleToClnicList(schedule);
        log.info(scheduleToClnicList.toString());
        //获取当前时间加30分钟
        //获取插入时间
        return insertClnicStaff(scheduleToClnicList);
    }
    /**
     * 每日固定时间处理
     */
    @Scheduled(cron = "${jobs.report.cron}", zone = "${jobs.timezone}")
    public void weeklyJob() {
        // 执行你的业务逻辑
        Boolean b = redisTemplate.opsForValue().setIfAbsent("weeklyJob", "run", 10, TimeUnit.SECONDS);
        if (Boolean.TRUE.equals(b)){
            timeConvert(dayNum);
        }
        log.info("weeklyJob run at {}", java.time.LocalDateTime.now());
    }
    /**
     * 根据未来几天插入
     * @param dayNum
     * @return
     */
    @Override
    public List<ClnicStaff> timeConvertByDayNum(int dayNum) {
        List<ClnicStaff> clnicStaffs = new ArrayList<>();
        for (int i = 0; i < dayNum; i++) {
            List<ClnicStaff> l = timeConvert(i);
            clnicStaffs.addAll(l);
        }
        return clnicStaffs;
    }

}
